package com.situ.blog.dao.impl;

import com.situ.blog.dao.IAdminDao;
import com.situ.blog.pojo.Admin;
import com.situ.blog.pojo.Auth;
import com.situ.blog.pojo.query.AdminQuery;
import com.situ.blog.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class AdminDao implements IAdminDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Admin> selectByPage(AdminQuery adminQuery) {
        List<Object> queryList = new ArrayList<>();

        String sql = "SELECT id,name,password,role,image,status,is_deleted,create_time,update_time FROM admin WHERE 1=1";
        String queryName = adminQuery.getName();
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = adminQuery.getRole();
        if (queryRole != null && !"".equals(queryRole)) {
            sql += " AND type_id=?";
            queryList.add(queryRole);
        }
        String beginCreateTime = adminQuery.getBeginCreateTime();
        String endCreateTime = adminQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null ) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        sql+=" LIMIT ?,?";

        int offset = (adminQuery.getPage()-1)*adminQuery.getLimit();
        queryList.add(offset);
        queryList.add(adminQuery.getLimit());

        return template.query(sql,new BeanPropertyRowMapper<>(Admin.class),queryList.toArray());
    }

    @Override
    public int totalCount(AdminQuery adminQuery) {
        List<Object> queryList = new ArrayList<>();
        String queryName = adminQuery.getName();
        String sql = "SELECT count(*) FROM admin WHERE 1=1";
        if (queryName != null && !"".equals(queryName)) {
            sql += " AND name LIKE ?";
            queryList.add("%" + queryName + "%");
        }
        Integer queryRole = adminQuery.getRole();
        if (queryRole != null && !"".equals(queryRole)) {
            sql += " AND type_id=?";
            queryList.add(queryRole);
        }
        String beginCreateTime = adminQuery.getBeginCreateTime();
        String endCreateTime = adminQuery.getEndCreateTime();
        if (beginCreateTime != null && endCreateTime != null && !"".equals(beginCreateTime)&& !"".equals(endCreateTime)) {
            sql += " AND create_time BETWEEN ? AND ?";
            queryList.add(beginCreateTime);
            queryList.add(endCreateTime);
        }
        return template.queryForObject(sql, Integer.class, queryList.toArray());
    }

    @Override
    public void add(Admin admin) {
        System.out.println("AdminDaoImpl.add");
        System.out.println(admin);

        String sql = "INSERT INTO admin(name,image,role,status) VALUES (?,?,?,?)";
        template.update(sql,admin.getName(),admin.getImage(),admin.getRole(),admin.getStatus());


    }
    public int deleteById(int id) {
        System.out.println("删除函数内传过来的id是："+id);

        int count = 0;

        String sql = "DELETE FROM admin WHERE id=?";
        count = template.update(sql,id);

        System.out.println("count:"+count);
        return count;
    }

    @Override
    public Admin selectById(String id) {
        System.out.println("AdminDaoImpl.selectById");
        String sql = "SELECT id,name,image,click_count,content,type_id,status FROM admin WHERE id=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class),id);
    }

    @Override
    public void updateStatus(String id,int status) {
        //写sql语句
        String sql = "UPDATE admin SET status=? WHERE id=?";
        template.update(sql,status,id);
    }

    @Override
    public void update(Admin admin) {
        //写sql语句
        String sql = "UPDATE admin SET name=?,role=?,image=? WHERE id=?";
        template.update(sql,admin.getName(),admin.getImage(),admin.getId());
    }

    @Override
    public Admin login(String name, String password) {
        String sql = "SELECT id,name,password,role,image,status,is_deleted,create_time,update_time FROM admin WHERE name=? AND password=?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), name, password);
    }


}
